package com.qingsong.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.qingsong.dto.*;
import com.qingsong.entity.Emp;
import com.qingsong.excel.EmpExcel;
import com.qingsong.result.Result;
import com.qingsong.service.EmpService;
import com.qingsong.util.EasyExcelUtil;
import com.qingsong.util.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author qingsong
 */
@Tag(name="员工模块")
@RestController
@RequestMapping("/api/v1/emp")
public class EmpController {
    @Autowired
    private EmpService empService;

    @Operation(summary = "单增一条记录")
    @PostMapping("save")
    public Object save(@RequestBody @Validated EmpInsertDTO empInsertDTO){
        return empService.insert(empInsertDTO);
    }

    @Operation(summary = "单查一条记录")
    @GetMapping("getInfo/{id}")
    public Object getInfo(@PathVariable Long id){
        return empService.selectById(id);
    }

    @Operation(summary = "修改一条记录")
    @PostMapping("update")
    public Object update(@RequestBody @Validated EmpUpdateDTO empUpdateDTO){
        return empService.updateById(empUpdateDTO);
    }

    @Operation(summary = "删除一条记录")
    @PostMapping ("/remove/{id}")
    public Object remove(@PathVariable Long id){
        return empService.logicDeleteById(id);
    }

    @Operation(summary = "批量删除多条记录")
    @PostMapping ("removeBatch")
    public Object removeBatch(@RequestParam List<Long> ids){
        return empService.logicDeleteByIds(ids);
    }

    @Operation(summary = "查询全部记录")
    @GetMapping ("list")
    public Object list(){
        return empService.list();
    }

    @Operation(summary = "分页查询记录")
    @PostMapping ("page")
    public Object page(@RequestBody @Validated PageDTO pageDTO){
        return empService.page(pageDTO);
    }

    @Operation(summary = "账号密码登录")
    @PostMapping("/loginByAccount")
    public Object loginByAccount(@RequestBody @Validated LoginDTO dto){
        return empService.loginByAccount(dto);
    }

    @Operation(summary = "打印数据报表")
    @SneakyThrows
    @GetMapping("/excel")
    public void excel(HttpServletResponse resp) {
        List<EmpExcel> empExcels = new ArrayList<>();
        List<Emp> emps = empService.list();
        for (Emp emp : emps) {
            EmpExcel empExcel = BeanUtil.copyProperties(emp, EmpExcel.class);
            empExcel.setGender(emp.getGender() == 0 ? "男" : emp.getGender() ==1 ? "女" : "保密");
            empExcels.add(empExcel);
        }
        EasyExcelUtil.download(resp, "员工统计表.xlsx", "sheet01",
                EmpExcel.class, empExcels);
    }

    @Operation(summary = "按员工名搜索")
    @PostMapping("pageLikeNicknameApi")
    public Object pageLikeNicknameApi(@RequestBody @Validated EmpPageDTO dto){
        return empService.pageLikeTitle(dto);
    }

    //上传文件不是JSON参数,而是二进制参数,不能用 @RequestBody
    @Operation(summary = "上传员工头像")
    @PostMapping("uploadAvatar")
    public Object uploadAvatar(EmpUploadDTO dto){
        return new Result<>(empService.uploadAvatar(dto));
    }

    @Operation(summary = "重置员工密码")
    @PostMapping("resetPassword/{id}")
    public Object resetPassword(@PathVariable Long id) {
        return new Result<>(empService.resetPassword(id));
    }

    @Operation(summary = "获取全部角色")
    @GetMapping("getRoles/{id}")
    public Object getRoles(@PathVariable Long id) {
        return empService.getRoles(id);
    }

    @Operation(summary = "我的角色列表")
    @PostMapping("updateRoles")
    public Object updateRoles(@RequestBody EmpUpdateRolesDTO dto) {
        return empService.updateRoles(dto);
    }

    @Operation(summary = "修改我的密码")
    @PostMapping("updatePassword")
    public Object updatePassword(@RequestBody EmpUpdatePasswordDTO dto,@RequestHeader("token") String token){
        dto.setId(JwtUtil.getId(token));
        return empService.updatePassword(dto);
    }
}
